//
//  LZEssenceViewController.m
//  LZBS
//
//  Created by 郝庆 on 16/6/10.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZEssenceViewController.h"
#import "LZTitleButton.h"
#import "LZAllViewController.h"
#import "LZVideoViewController.h"
#import "LZVoiceViewController.h"
#import "LZPictureViewController.h"
#import "LZWordViewController.h"
#import "LZRecommendTagViewController.h"

@interface LZEssenceViewController ()<UIScrollViewDelegate>
/** 标题按钮底部的指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的标题按钮 */
@property (nonatomic, weak) LZTitleButton *selectedTitleButton;
/** UIScrollView */
@property (nonatomic, weak) UIScrollView *scrollView;
/** 标题栏 */
@property (nonatomic, weak) UIView *titleView;
@end

@implementation LZEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNaVc];
    
    [self setUpChildVc];
    
    [self setUpScrollView];
    
    [self setUpTitleView];
    
    // 默认添加子控制器的view
    [self addChildVcView];
}

#pragma mark - 创建所有的子控制器
- (void)setUpChildVc
{
    LZAllViewController *allVc = [[LZAllViewController alloc] init];
    [self addChildViewController:allVc];
    
    LZVideoViewController *videoVc = [[LZVideoViewController alloc] init];
    [self addChildViewController:videoVc];
    
    LZVoiceViewController *voiceVc = [[LZVoiceViewController alloc] init];
    [self addChildViewController:voiceVc];
    
    LZPictureViewController *pictureVc = [[LZPictureViewController alloc] init];
    [self addChildViewController:pictureVc];
    
    LZWordViewController *wordVc = [[LZWordViewController alloc] init];
    [self addChildViewController:wordVc];
}

#pragma mark - 添加子控制器的view
- (void)addChildVcView
{
    // 子控制器的索引
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.lz_width;
    
    // 取出子控制器
    UIViewController *childVc = self.childViewControllers[index];
    
    // 已经添加过了就没必要再添加
    if (childVc.view.superview) return;
    
    childVc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVc.view];
}

- (void)setUpScrollView
{
    // 不允许自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 设置scrollView的contentSize
    scrollView.contentSize = CGSizeMake(self.childViewControllers.count * scrollView.lz_width, 0);
    self.scrollView = scrollView;
}

#pragma mark - 设置标题控件
- (void)setUpTitleView
{
    // 设置标题栏
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.lz_width, 40)];
    titleView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.7];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    // 添加标题栏的内容
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    NSUInteger count = titles.count;
    CGFloat titleBtnW = titleView.lz_width / count;
    CGFloat titleBtnH = titleView.lz_height;
    CGFloat titleBtnY = 0;
    for (NSUInteger i = 0; i < count; i++) {
        CGFloat titleBtnX = i * titleBtnW;
        LZTitleButton *titleBtn = [LZTitleButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setTitle:titles[i] forState:UIControlStateNormal];
        titleBtn.tag = i;
        titleBtn.frame = CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH);
        [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchDown];
        [titleView addSubview:titleBtn];
    }
    
    // 按钮的选中颜色
    LZTitleButton *firstTitleButton = titleView.subviews.firstObject;
    
    // 底部的指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    indicatorView.lz_height = 1.5;
    indicatorView.lz_y = titleView.lz_height - indicatorView.lz_height;
    [titleView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 立刻根据文字内容计算label宽度
    [firstTitleButton.titleLabel sizeToFit];
    self.indicatorView.lz_width = firstTitleButton.titleLabel.lz_width;
    self.indicatorView.lz_centerX = firstTitleButton.lz_centerX;
    
    // 默认选中第一个按钮
    firstTitleButton.selected = YES;
    self.selectedTitleButton = firstTitleButton;
    
}

#pragma mark - 设置导航控制器的相关属性
- (void)setUpNaVc
{
    self.view.backgroundColor = LZCommonBgColor;
    
    // 设置中间标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    //设置左边按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem lz_itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(leftClick)];
    
}

#pragma mark - 点击事件
- (void)leftClick
{
    LZRecommendTagViewController *tag = [[LZRecommendTagViewController alloc] init];
    [self.navigationController pushViewController:tag animated:YES];
}

- (void)titleBtnClick:(LZTitleButton *)titleBtn
{
    // 某个标题按钮被重复点击
    if (titleBtn == self.selectedTitleButton) {
        [[NSNotificationCenter defaultCenter] postNotificationName:LZTitleButtonDidRepeatClickNotification object:nil];
    }
     
    // 控制按钮状态
    self.selectedTitleButton.selected = NO;
    titleBtn.selected = YES;
    self.selectedTitleButton = titleBtn;
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.lz_width = titleBtn.titleLabel.lz_width;
        self.indicatorView.lz_centerX = titleBtn.lz_centerX;
    }];
    
    // 让UIScrollView滚动到对应位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = titleBtn.tag * self.scrollView.lz_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self addChildVcView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 选中\点击对应的按钮
    NSUInteger index = scrollView.contentOffset.x / scrollView.lz_width;
    LZTitleButton *titleButton = self.titleView.subviews[index];
    [self titleBtnClick:titleButton];
    
    // 添加子控制器的view
    [self addChildVcView];
}
@end
